# koroFileHeader at Home PC
# Create: 2021-04-10 18:01:00
# LastEdit: 2021-04-11 10:17:32
"""直接提供订阅（subscription_id）查询"""
__author__ = '749B'

import os, sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 
sys.path.append(BASE_DIR)

import azure.mgmt.resourcegraph as arg
from msrestazure.azure_cloud import AZURE_CHINA_CLOUD as CLOUD
#from azure.mgmt.resourcegraph import ResourceGraphClient
# pylint: disable = import-error
from learning.source_code_modify.my_resource_graph_client import MyResourceGraphClient as ResourceGraphClient
from azure.identity import DefaultAzureCredential

# 加载环境变量
from environs import Env
env = Env()
env.read_env()

subscriptions = [i for i in env.dict('SUBSCRIPTIONS').values()]

credential = DefaultAzureCredential(authority=CLOUD.endpoints.active_directory)

resource_graph_client = ResourceGraphClient(
    credential,
    base_url=CLOUD.endpoints.resource_manager,
    credential_scopes=[CLOUD.endpoints.resource_manager + "/.default"])

# 请求的部分
# Create Azure Resource Graph client and set options
argQueryOptions = arg.models.QueryRequestOptions(result_format="objectArray")  # "table" or "objectArray"

strQuery = "Resources | summarize count()"

argQuery = arg.models.QueryRequest(subscriptions=subscriptions, query=strQuery, options=argQueryOptions)
argResults = resource_graph_client.resources(argQuery)
print(argResults.data)

argQuery = arg.models.QueryRequest(subscriptions=subscriptions[1:], query=strQuery, options=argQueryOptions)
argResults = resource_graph_client.resources(argQuery)
print(argResults.data)

argQuery = arg.models.QueryRequest(subscriptions=subscriptions[:1], query=strQuery, options=argQueryOptions)
argResults = resource_graph_client.resources(argQuery)
print(argResults.data)

